package net.alche.sdd.servlet.biz;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.alche.al.util.Far;
import net.alche.al.util.M;
import net.alche.al.util.far.pojo.VueFunction;
import net.alche.sdd.fatis.SddFatis;

@SuppressWarnings("all")
public class SddApplicatonBiz {
  public static final Logger logger = LoggerFactory.getLogger(SddApplicatonBiz.class);
  public Map<String, Object> beanMap = null;
  Map<String, String> applicationConf = null;

  SddFatis sddFatis = null;
  VueFunction funHttpVisit = null;

  public void setConf(Map<String, String> conf) {
    applicationConf = conf;
    initStore();
  }

  public void initSddFatis() {
    if (getBool("jdbc.noInitDatabase")) {
      logger.info("no init database!");
    } else {
      initFatis();
      if (fatis().testLink()) {
        logger.info("test jdbc connect ok!");
      } else {
        logger.error("load jdbc [failed]!");
      }
    }
  }

  public boolean hasKey(String key) {
    return applicationConf.get(key) != null;
  }

  // null->""
  public String getStr(String key) {
    return M.getStr(applicationConf, key);
  }

  public Integer getInt(String key) {
    String val = getStr(key);
    if (val == null) {
      return null;
    }
    return Integer.parseInt(val);
  }

  public Boolean getBool(String key) {
    String val = getStr(key);
    if (val == null) {
      return null;
    }
    return "true".equals(val);
  }

  public SddFatis fatis() {
    return sddFatis;
  }

  public void initFatis() {
    sddFatis = new SddFatis();
    Map mp = new HashMap<>();
    String[] keys = new String[] { "driver", "url", "username", "password", "maxActive", "initialSize", "minIdle",
        "maxWait", "testWhileIdle", "testOnBorrow", "testOnReturn", "poolPreparedStatements",
        "breakAfterAcquireFailure", "logSql", "logSqlNoSelect", "dbType" };
    for (String key : keys) {
      if (hasKey("jdbc." + key))
        mp.put(key, getStr("jdbc." + key));
    }
    sddFatis.init(mp);
  }

  public void initStore() {
    Map<String, String> mp = new HashMap<>();
    // String[] keys = new String[] { "serSign", "serIp", "serPort", "serToken" };
    Set<String> keys = applicationConf.keySet();
    for (String key : keys) {
      // if (hasKey("bridge." + key))
      if (key.startsWith("bridge.")) {
        mp.put(M.subSuf(key, "."), getStr(key));
      }
    }
    Far.store.serConf = mp;
  }

  public VueFunction setFunHttpVisit() {
    VueFunction fun = new VueFunction();
    this.funHttpVisit = fun;
    return fun;
  }

  public void logHttpSec(String type, String path, long secBegin) {
    if (this.funHttpVisit != null) {
      int use = (int) (Far.tl.getCurrentTimeMillis() - secBegin);
      try {
        this.funHttpVisit.callVals(type, path, use);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }

  public <T> T getBean(String beanName) {
    return (T) beanMap.get(beanName);
  }
}
